
// HEX转RGB
export const hexToRgb = (hex) => {
  hex = hex.replace('#', '');
  const rgb = hex.match(/../g).map(v => parseInt(v, 16));
  return rgb;
};

// RGB转HEX
export const rgbToHex = (r, g, b) => {
  return `#${[r, g, b].map(v => v.toString(16).padStart(2, '0')).join('')}`;
};

// 颜色变亮
export const lightenColor = (color, level) => {
  const rgb = hexToRgb(color);
  for (let i = 0; i < 3; i++) {
    rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i]);
  }
  return rgbToHex(...rgb);
};

// 颜色变暗
export const darkenColor = (color, level) => {
  const rgb = hexToRgb(color);
  for (let i = 0; i < 3; i++) {
    rgb[i] = Math.floor(rgb[i] * (1 - level));
  }
  return rgbToHex(...rgb);
};


export const setTheme = (hexColor) => {
  document.documentElement.style.setProperty("--el-color-primary", hexColor);
  // 生成浅色系
  for (let i = 1; i <= 9; i++) {
    document.documentElement.style.setProperty(
      `--el-color-primary-light-${i}`,
      lightenColor(hexColor, i / 10)
    );
  }
  // 生成深色系
  for (let i = 1; i <= 9; i++) {
    document.documentElement.style.setProperty(
      `--el-color-primary-dark-${i}`,
      darkenColor(hexColor, i / 10)
    );
  }
};